package com.ken.controller.admin;

import cn.hutool.core.lang.UUID;
import com.ken.constant.MessageConstant;
import com.ken.result.Result;
import com.ken.utils.AliOssUtil;
import com.ken.result.Result;
import com.ken.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/admin/common")
@Slf4j
@Api(tags = "通用接口")
public class CommonController {
    @Autowired
    private AliOssUtil aliOss;
    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传接口")
    public Result<String> upload(MultipartFile file){
        try {
            log.info("文件上传{}",file);
            String originalFilename = file.getOriginalFilename();
            if (originalFilename == null) {
                return Result.error(MessageConstant.UPLOAD_FAILED);
            }
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            String objectName= UUID.randomUUID().toString(true)+substring;
            String upload = aliOss.upload(file.getBytes(), objectName);
            return Result.success(upload);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
